﻿using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.LogDomain;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.PlcConnectPoolDomain;
using Zocono.WCS.Domain.ProcessModuleDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.PlcCommon;
using Zocono.WCS.WMSApiDomain.WebApi;

namespace Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage
{
    public class PlanarBatteryScanBandingService : IPlanarGeneralPlcActionProcessService
    {
        private readonly ISnowflake _snowflake;
        private readonly IPlcConnectionPool _PlcConnectionPool;
        private readonly IPlcWriteDbInfoRepository _PlcWriteDbInfoRepository;
        private readonly ITelegramRecordInfoRepository _TelegramRecordInfoRepository;
        private readonly IWorkTaskInfoRepository _workTaskInfoRepository;
        private readonly IPlcConnectionStateInfoRepository _IPlcConnectionStateInfoRepository;
        private readonly IRunLogInfoService _IRunLogInfoService;
        private readonly IPlcReadDbInfoRepository _PlcReadeDbInfoRepository;
        public PlanarBatteryScanBandingService(ISnowflake snowflake, IPlcConnectionPool plcConnectionPool
            , IPlcWriteDbInfoRepository plcWriteDbInfoRepository
            , ITelegramRecordInfoRepository telegramRecordInfoRepository
            , IWorkTaskInfoRepository workTaskInfoRepository
                , IPlcConnectionStateInfoRepository plcConnectionStateInfoRepository
            , IRunLogInfoService iRunLogInfoService, IPlcReadDbInfoRepository plcReadDbInfoRepository)
        {
            _snowflake = snowflake;
            _PlcConnectionPool = plcConnectionPool;
            _PlcWriteDbInfoRepository = plcWriteDbInfoRepository;
            _TelegramRecordInfoRepository = telegramRecordInfoRepository;
            _workTaskInfoRepository = workTaskInfoRepository;
            _IPlcConnectionStateInfoRepository = plcConnectionStateInfoRepository;
            _IRunLogInfoService = iRunLogInfoService;
            _PlcReadeDbInfoRepository = plcReadDbInfoRepository;
        }

        public async Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, BytesObjectBase wcsActionMessage, EquipmentDbObject planarEquipmentDbObject)
        {

            var plcMessage = plcActionMessage as PlanarGeneralPlcActionBO;//Plc的动作报文
            var wcsMessage = wcsActionMessage as PlanarGeneralWcsActionBO;//Wcs的动作报文
            plcMessage.PalletBarcode = plcMessage.PalletBarcode.Trim();

            RunLogInfo runLogInfo = new RunLogInfo();
            runLogInfo.ContainerBarcode = plcMessage.PalletBarcode;
            runLogInfo.EquipmentCode = plcMessage.EquipmentCode;
            runLogInfo.RunType = "巷道/目标位请求";

            //var plcStateBo = plcStateMassge as PlcOnLineStateBO;//Plc在线离线报文 没有设备信息
            PlanarGeneralWriteWcsAction writeBo = null;//写入Plc的报文



            PlcConnectionS7 plcConnect = _PlcConnectionPool.S7GetWritePlcConnection(plcMessage.PlcConnectionID); ;//plc写入连接（不锁定）

            var plcIPStates = await _IPlcConnectionStateInfoRepository.GetFirstAsync(l => l.PlcConnectionID == plcMessage.PlcConnectionID && l.Connected == true);
            if (plcIPStates == null)
            {
                runLogInfo.RunLog = $"离线：请求分配失败：{plcConnect.IP}:plc已离线";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }

            //添加报文记录
            //var telegramUniqueIDJson = JsonConvert.SerializeObject(telegramUniqueIDObj);


            //1. 验证plc是否在线
            //if (plcStateBo.PlcState == 0)
            //{
            //    _LogUtil.Info($"[平面设备扫码报文服务]处理失败：设备({scanPlcMessage.EquipmentCode})PLC({plcConnect.IP})已离线");
            //    return;
            //}

            //4. 组织报文，写入plc
            var objectType = typeof(PlanarGeneralWriteWcsAction).Name;
            var writeConfig = await _PlcWriteDbInfoRepository.GetFirstAsync(s => s.EquipmentCode == plcMessage.EquipmentCode && s.ObjectType == objectType);
            if (writeConfig == null)
            {
                runLogInfo.RunLog = "获取PLC写入配置失败";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }

          

            //plc报文是什么握手类型则回什么握手类型
            writeBo = new PlanarGeneralWriteWcsAction(
                writeConfig.PlcConnectionId,
                writeConfig.ID,
                writeConfig.DbCode,
                writeConfig.DbStart,
                objectType,
                writeConfig.EquipmentCode.ToInt32(),
                plcMessage.ActionType,
                plcMessage.ToEquipmentCode,
                plcMessage.TaskCode,
                plcMessage.PalletBarcode,
                plcMessage.PalletType,
                alrmCode: plcMessage.AlrmCode);

           

            var plcConn = _PlcConnectionPool.S7GetWritePlcConnection(writeBo.PlcConnectionID);
            if (plcConn == null)
            {
                runLogInfo.RunLog = $"获取连接({writeBo.PlcConnectionID})失败";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }
            //托盘号不存在 写入握手类型100报警
            if (string.IsNullOrEmpty(plcMessage.PalletBarcode))
            {

                runLogInfo.RunLog = "PLC托盘信息不存在！";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                writeBo.ActionType = 0;
                writeBo.AlrmCode = 10;
                var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());

                if (writeBl == false)
                {
                    runLogInfo.RunLog = $"PLC(IP{plcConn.IP}：写入报文失败";
                    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                }
                else
                {
                    runLogInfo.RunLog = $"PLC(IP{plcConn.IP}：写入报文成功";
                    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                }
                return;
            }
            //托盘类型
            switch (writeBo.PalletBarcode.Substring(1, 1))
            {
                case "A":
                    writeBo.PalletType = 2;
                    break;
                case "B":
                    writeBo.PalletType = 1;
                    break;
                case "C":
                    writeBo.PalletType = 3;
                    break;
            }

            if (plcMessage.ActionType == 40)
            {
                //给plc数量和电池类型（向WMS获取）

                //把托盘给到WMS然后获取数量和电池类型



                writeBo.Reserve3 = 1;
                writeBo.Reserve4 = 1;
                writeBo.ActionType = 41;
                var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());

                return;
            }


            //托盘号不存在 写入握手类型100报警
            if (string.IsNullOrEmpty(plcMessage.PalletBarcode))
            {
                runLogInfo.RunLog = $"[平面设备扫码报文服务]请求巷道分配失败：设备编号{writeBo.EquipmentCode}托盘信息不存在！";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                return;
            }


            var plcConfigBattery = await _PlcReadeDbInfoRepository.GetFirstAsync(l => l.EquipmentCode == plcMessage.EquipmentCode && l.DbCode == 102);


            byte[] bytesdo = await plcConnect.ReadDbAsync(plcConfigBattery.DbCode, plcConfigBattery.DbStart, plcConfigBattery.DbLength);
            if (bytesdo == null)
            {
                runLogInfo.RunLog = $"读DB失败";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }

            PlanarBatteryScanPlcActionBO batteryScanPlcAction = new();
            batteryScanPlcAction.Serialize(plcConfigBattery.PlcConnectionID, plcConfigBattery.ID, plcConfigBattery.DbStart, plcConfigBattery.DbLength, plcConfigBattery.DbStart, plcConfigBattery.DbLength, "PlanarBatteryScanPlcActionBO", plcConfigBattery.EquipmentCode, bytesdo);//主要是bytes转化成明文


            #region 判断条码为空或存在重码提示

            var batteryCodeList = new List<string>
            {
                batteryScanPlcAction.BatteryCode1,
                batteryScanPlcAction.BatteryCode2,
                batteryScanPlcAction.BatteryCode3,
                batteryScanPlcAction.BatteryCode4,
                batteryScanPlcAction.BatteryCode5,
                batteryScanPlcAction.BatteryCode6,
                batteryScanPlcAction.BatteryCode7,
                batteryScanPlcAction.BatteryCode8,
                batteryScanPlcAction.BatteryCode9,
                batteryScanPlcAction.BatteryCode10,
                batteryScanPlcAction.BatteryCode11,
                batteryScanPlcAction.BatteryCode12,
                batteryScanPlcAction.BatteryCode13,
                batteryScanPlcAction.BatteryCode14,
                batteryScanPlcAction.BatteryCode15,
                batteryScanPlcAction.BatteryCode16,
                batteryScanPlcAction.BatteryCode17,
                batteryScanPlcAction.BatteryCode18,
                batteryScanPlcAction.BatteryCode19,
                batteryScanPlcAction.BatteryCode20,
                batteryScanPlcAction.BatteryCode21,
                batteryScanPlcAction.BatteryCode22,
                batteryScanPlcAction.BatteryCode23,
                batteryScanPlcAction.BatteryCode24,
                batteryScanPlcAction.BatteryCode25,
                batteryScanPlcAction.BatteryCode26,
                batteryScanPlcAction.BatteryCode27,
                batteryScanPlcAction.BatteryCode28,
                batteryScanPlcAction.BatteryCode29,
                batteryScanPlcAction.BatteryCode30,
                batteryScanPlcAction.BatteryCode31,
                batteryScanPlcAction.BatteryCode32,
                batteryScanPlcAction.BatteryCode33,
                batteryScanPlcAction.BatteryCode34,
                batteryScanPlcAction.BatteryCode35,
                batteryScanPlcAction.BatteryCode36,
                batteryScanPlcAction.BatteryCode37,
                batteryScanPlcAction.BatteryCode38,
                batteryScanPlcAction.BatteryCode39,
                batteryScanPlcAction.BatteryCode40,
            };

            ////判断电池条码缺失或存在重码
            //if (batteryCodeList.Where(s => !string.IsNullOrEmpty(s)).Distinct().Count() < 38)// Distinct不同的
            //{
            //    var batteryJson = JsonConvert.SerializeObject(batteryCodeList);
            //    runLogInfo.RunLog = $"判断电池条码缺失或存在重码:{batteryJson}";
            //    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            //    return;
            //}

            #endregion

            try
            {
                WCSCombinePalletInfoDto wCSCombinePalletInfoDto = new WCSCombinePalletInfoDto();
                wCSCombinePalletInfoDto.ContainerBarcode = plcMessage.PalletBarcode;
                wCSCombinePalletInfoDto.EquipmentCode = plcMessage.EquipmentCode;

                List<WCSOneBatteryDto> list = GetBatteryDto(batteryScanPlcAction);
                wCSCombinePalletInfoDto.oneBatteryInfoDtos = list;
                //组盘提交数据到WMS
                var bo = await WCSMessageWithWMSApi.WCSAddComPalletAsync(wCSCombinePalletInfoDto);

                if (!bo)
                {
                    writeBo.ActionType = 5;
                    var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());

                    runLogInfo.RunLog = $"[平面设备扫码报文服务]处理失败：设备号({plcMessage.EquipmentCode})扫码组盘失败";
                    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                    return;
                }

            }
            catch (Exception ex)
            {
                runLogInfo.RunLog = $"[平面设备扫码报文服务]提交数据到WMS返回异常";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            }


            #region 分配巷道
            //判断设备在wcs是否已存在分配巷道任务
            var exp = Expressionable.Create<WorkTaskInfo>();
            exp.AndIF(true, (a) => a.PalletCode == plcMessage.PalletBarcode && (a.TaskState == EnumTaskState.Initial || a.TaskState == EnumTaskState.Executing));
            var wcsWorkTaskInfo = await _workTaskInfoRepository.GetFirstAsync(exp.ToExpression());
            //根据托盘是否存在其它的任务 && 起点位置是否相同判断，存在则写入正常报文Return
            if (wcsWorkTaskInfo != null /*&& wcsWorkTaskInfo.CurrentFromEquipment == plcMessage.EquipmentCode*/)
            {

                runLogInfo.RunLog = $"请求分配重复:任务号：{wcsWorkTaskInfo.TaskCode}入库任务已存在";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                //针对wcs任务为初始化的处理成执行中并同步更新wms任务状态
                if (wcsWorkTaskInfo.TaskState == EnumTaskState.Initial || wcsWorkTaskInfo.TaskState == EnumTaskState.Executing)
                {
                    wcsWorkTaskInfo.TaskState = EnumTaskState.Executing;
                    wcsWorkTaskInfo.UpdateTime = DateTime.Now;
                    wcsWorkTaskInfo.CurrentDetailTaskState = EnumDetailTaskState.Executing;
                    try
                    {
                        //同步更新wms任务状态
                        var flag = await WCSMessageWithWMSApi.WCSUpdateWMStaskStateAndProcessTaskStateAsync(wcsWorkTaskInfo.WmsTaskCode, (int)EnumTaskState.Executing);
                        if (flag)
                        {
                            wcsWorkTaskInfo.UploadWmsState = EnumUploadWmsState.Succeed;
                            wcsWorkTaskInfo.UploadWmsTime = DateTime.Now;
                        }
                    }
                    catch (Exception ex)
                    {

                        runLogInfo.RunLog = $"wms任务号{wcsWorkTaskInfo.TaskCode}同步状态更新失败";
                        await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                    }
                    //更新wcs任务状态为执行中
                    await _workTaskInfoRepository.UpdateAsync(wcsWorkTaskInfo);
                }

                //写入正常报文
                writeBo.TaskCode = wcsWorkTaskInfo.TaskCode;
                writeBo.PalletBarcode = wcsWorkTaskInfo.PalletCode;
                writeBo.ToEquipmentCode = wcsWorkTaskInfo.CurrentToEquipment.ToInt32();
                var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());
                if (!writeBl)
                {
                    runLogInfo.RunLog = $"请求分配重复:写入正常报文报文失败";
                    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                    return;
                }
                else
                {
                    runLogInfo.RunLog = $"请求分配重复:写入正常报文报文成功";
                    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                }
                return;
            }
            #endregion

            #region wcs托盘上架任务NA逻辑处理
            WmsTaskInfoDto workTaskDto = null;
            try
            {
                //向wms读取巷道分配任务
                WMSApiDomain.WebApi.HeaderResult<WmsTaskInfoDto> res = await WCSMessageWithWMSApi.WCSCreatUploadWarehouseWMSTaskAsync(plcMessage.PalletBarcode, plcMessage.EquipmentCode);
                if (res.IsSucceed)
                {
                    workTaskDto = res.Result;
                }
                else
                {
                    var msg = res.Message;
                    runLogInfo.RunLog = $"向Wms请求任务失败:{msg}";
                    await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                    return;
                }

            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("at") && ex.Message.Contains("System.Exception:"))
                {

                    var msg = ex.Message.Split("at").First().Split("System.Exception:");
                    runLogInfo.RunLog = $"向Wms请求任务失败:{msg[1]}";

                }
                else
                {
                    var msg = ex.Message.Length > 150 ? ex.Message.Substring(0, 100) : ex.Message;
                    runLogInfo.RunLog = $"向Wms请求任务失败:{msg}";
                }
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            }

            //wms请求失败处理 写入100报警握手类型
            if (workTaskDto == null)
            {

                runLogInfo.RunLog = $"向Wms请求任务失败WMS返回空";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                writeBo.ActionType = 0;
                writeBo.AlrmCode = 100;
                var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());
                if (writeBl == false)
                {
                    runLogInfo.RunLog = $"向Wms请求任务失败：向PLC写入报文失败";
                }
                else
                {
                    runLogInfo.RunLog = $"向Wms请求任务失败：向PLC写入报文成功";
                }
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }

            //WorkTaskInfo数据补全
            var workTaskInfo = new WorkTaskInfo();

            long id = _snowflake.GetId();//雪花ID
            workTaskInfo.ID = id;
            //wcs任务code从wmstaskcode日开始截取
            int.TryParse(workTaskDto.TaskCode.ToString().Substring(6), out var wcsTaskCode);
            workTaskInfo.TaskCode = wcsTaskCode;
            //wms赋值
            workTaskInfo.WmsTaskCode = workTaskDto.TaskCode;
            workTaskInfo.DependWmsTaskCode = workTaskDto.DependWMSTaskCode;
            workTaskInfo.TaskType = Enum.Parse<EnumTaskType>(workTaskDto.TaskType.ToString());

            workTaskInfo.WmsFromPosition = workTaskDto.StartPosition;
            workTaskInfo.WmsToPosition = workTaskDto.EndPosition;
            workTaskInfo.WmsFromStation = workTaskDto.FromStation;
            workTaskInfo.WmsToStation = workTaskDto.ToStation;
            workTaskInfo.TaskProcessState = EnumTaskProcessState.Initial;
            workTaskInfo.TaskState = EnumTaskState.Executing;
            workTaskInfo.Priority = workTaskDto.Priority;

            //托盘条码
            workTaskInfo.PalletCode = plcMessage.PalletBarcode.Trim();




            #region 任务子任务信息赋值by RouteDetailInfo
            //获取配置巷道信息
            var routeConfigExp = Expressionable.Create<RouteInfo>();
            routeConfigExp.AndIF(true, (a) => a.FromStation == workTaskInfo.WmsFromStation && a.ToStation == workTaskInfo.WmsToStation);
            var routeInfo = await _workTaskInfoRepository.AsSugarClient()
                .Queryable<RouteInfo>()
                .Where(routeConfigExp.ToExpression())
                .Select((a) => new RouteInfo()
                {
                    ID = a.ID,
                    FromStation = a.FromStation,
                    ToStation = a.ToStation,
                }).FirstAsync();
            if (routeInfo == null)
            {
                runLogInfo.RunLog = $"起始工位{workTaskInfo.WmsFromStation}目标工位{workTaskInfo.WmsToStation}获取工位失败";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }

            //巷道详情信息获取
            var routeDetailConfigExp = Expressionable.Create<RouteDetailInfo>();
            routeDetailConfigExp.AndIF(true, (a) => a.RouteID == routeInfo.ID && a.IsStart);
            var routeDetailInfo = await _workTaskInfoRepository.AsSugarClient()
                .Queryable<RouteDetailInfo>()
                .Where(routeDetailConfigExp.ToExpression())
                .Select((a) => new
                {
                    a.ID,
                    a.RouteID,
                    a.FromCargo,
                    a.ToCargo,
                    a.DispatchType,
                    a.FromEquipment,
                    a.ToEquipment,

                }).FirstAsync();

            if (routeDetailInfo == null)
            {
                runLogInfo.RunLog = $"巷道路径详情配置信息起始工位{workTaskInfo.WmsFromStation}目标工位{workTaskInfo.WmsToStation}获取设备路径失败";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }

            workTaskInfo.CurrentToEquipment = routeDetailInfo.ToEquipment;
            workTaskInfo.CurrentFromCargo = routeDetailInfo.FromCargo;
            workTaskInfo.CurrentToCargo = routeDetailInfo.ToCargo;
            workTaskInfo.RouteID = routeDetailInfo.RouteID;
            workTaskInfo.CurrentRounteDetailID = routeDetailInfo.ID;
            workTaskInfo.CurrentDispatchType = routeDetailInfo.DispatchType;
            //起始设备位 
            workTaskInfo.CurrentFromEquipment = workTaskInfo.WmsFromPosition; //WMS 作业实际起点设备
                                                                              // workTaskInfo.CurrentToEquipment = routeDetailInfo.ToEquipment;
            workTaskInfo.CurrentDetailTaskState = EnumDetailTaskState.Executing;
            workTaskInfo.CurrentCreateTime = DateTime.Now;
            workTaskInfo.CurrentUpdateTime = DateTime.Now;
            #endregion

            workTaskInfo.SaveTime = DateTime.Now;
            workTaskInfo.UpdateTime = DateTime.Now;

            #region 同步更新wms任务状态&写入正常报文
            //写入正常报文到Plc
            writeBo.TaskCode = workTaskInfo.TaskCode;//wcs任务号
            writeBo.ToEquipmentCode = workTaskInfo.CurrentToEquipment.ToInt32();//目标设备编号

            runLogInfo.RunLog = $"PLC写入报文内容： {JsonConvert.SerializeObject(writeBo)}";
            await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            var writeResult = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());

            if (writeResult == false)
            {
                runLogInfo.RunLog = $"PLC(IP{plcConn.IP})写入报文失败";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }
            else
            {
                runLogInfo.RunLog = $"PLC(IP{plcConn.IP})写入报文成功";
                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            }


            try
            {
                //同步更新wms任务状态为执行中
                var flag = await WCSMessageWithWMSApi.WCSUpdateWMStaskStateAndProcessTaskStateAsync(workTaskInfo.WmsTaskCode, (int)EnumTaskState.Executing);
                if (flag)
                {
                    workTaskInfo.UploadWmsState = EnumUploadWmsState.Succeed;
                    workTaskInfo.UploadWmsTime = DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                runLogInfo.RunLog = $"任务号{workTaskInfo.WmsTaskCode}:向Wms同步状态更新失败";

                await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            }

            //WorkTaskInfo数据插入
            var expCheck = Expressionable.Create<WorkTaskInfo>();
            expCheck.AndIF(true, (a) => a.PalletCode == plcMessage.PalletBarcode && (a.TaskState == EnumTaskState.Initial || a.TaskState == EnumTaskState.Executing));
            var wcsCheckWorkTaskInfo = await _workTaskInfoRepository.GetFirstAsync(expCheck.ToExpression());
            if (wcsCheckWorkTaskInfo == null)
            {
                bool bl = await _workTaskInfoRepository.InsertAsync(workTaskInfo);
            }

            #endregion

            #endregion




        }




        public List<WCSOneBatteryDto> GetBatteryDto(PlanarBatteryScanPlcActionBO batteryScanPlcAction)
        {
            List<WCSOneBatteryDto> oneBatteryInfoDtos = new List<WCSOneBatteryDto>();
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 1,
                BatteryCode = batteryScanPlcAction.BatteryCode1
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 2,
                BatteryCode = batteryScanPlcAction.BatteryCode2
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 3,
                BatteryCode = batteryScanPlcAction.BatteryCode3
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 4,
                BatteryCode = batteryScanPlcAction.BatteryCode4
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 5,
                BatteryCode = batteryScanPlcAction.BatteryCode5
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 6,
                BatteryCode = batteryScanPlcAction.BatteryCode6
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 7,
                BatteryCode = batteryScanPlcAction.BatteryCode7
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 8,
                BatteryCode = batteryScanPlcAction.BatteryCode8
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 9,
                BatteryCode = batteryScanPlcAction.BatteryCode9
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 10,
                BatteryCode = batteryScanPlcAction.BatteryCode10
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 11,
                BatteryCode = batteryScanPlcAction.BatteryCode11
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 12,
                BatteryCode = batteryScanPlcAction.BatteryCode12
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 13,
                BatteryCode = batteryScanPlcAction.BatteryCode13
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 14,
                BatteryCode = batteryScanPlcAction.BatteryCode14
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 15,
                BatteryCode = batteryScanPlcAction.BatteryCode15
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 16,
                BatteryCode = batteryScanPlcAction.BatteryCode16
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 17,
                BatteryCode = batteryScanPlcAction.BatteryCode17
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 18,
                BatteryCode = batteryScanPlcAction.BatteryCode18
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 19,
                BatteryCode = batteryScanPlcAction.BatteryCode19
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 20,
                BatteryCode = batteryScanPlcAction.BatteryCode20
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 21,
                BatteryCode = batteryScanPlcAction.BatteryCode21
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 22,
                BatteryCode = batteryScanPlcAction.BatteryCode22
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 23,
                BatteryCode = batteryScanPlcAction.BatteryCode23
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 24,
                BatteryCode = batteryScanPlcAction.BatteryCode24
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 25,
                BatteryCode = batteryScanPlcAction.BatteryCode25
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 26,
                BatteryCode = batteryScanPlcAction.BatteryCode26
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 27,
                BatteryCode = batteryScanPlcAction.BatteryCode27
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 28,
                BatteryCode = batteryScanPlcAction.BatteryCode28
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 29,
                BatteryCode = batteryScanPlcAction.BatteryCode29
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 30,
                BatteryCode = batteryScanPlcAction.BatteryCode30
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 31,
                BatteryCode = batteryScanPlcAction.BatteryCode31
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 32,
                BatteryCode = batteryScanPlcAction.BatteryCode32
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 33,
                BatteryCode = batteryScanPlcAction.BatteryCode33
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 34,
                BatteryCode = batteryScanPlcAction.BatteryCode34
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 35,
                BatteryCode = batteryScanPlcAction.BatteryCode35
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 36,
                BatteryCode = batteryScanPlcAction.BatteryCode36
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 37,
                BatteryCode = batteryScanPlcAction.BatteryCode37
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 38,
                BatteryCode = batteryScanPlcAction.BatteryCode38
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 39,
                BatteryCode = batteryScanPlcAction.BatteryCode39
            });
            oneBatteryInfoDtos.Add(new WCSOneBatteryDto()
            {
                BatteryIndex = 40,
                BatteryCode = batteryScanPlcAction.BatteryCode40
            });

            return oneBatteryInfoDtos;
        }

    }
}

